import re

# 打开txt
def open_txt(txt_path, split=False):
    if split: # 列表['11','22',...]
        with open(txt_path, encoding="utf-8") as f:
            lines = f.read().splitlines()
    else: # 每行内容，包括\n
        with open(txt_path, encoding="utf-8") as f:
            lines = f.read()
    return lines


# 写入txt, 编码问题：https://www.cnblogs.com/baijing1/p/9885891.html
def write2txt(lines, txt_path, add=True):
    if add:
        with open(txt_path, 'a', encoding='utf-8') as file_handle:
            file_handle.write("{}\n".format(lines))
    else:
        with open(txt_path, 'w', encoding='utf-8') as file_handle:
            file_handle.write("{}\n".format(lines))


# 判断某行是否存在某些匹配项
def isMatch(strs, line):
    flag = 0
    for str in strs:
        if re.match(str, line):
            flag = 1
            break
    return flag